define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'layui', 'template'], function ($, undefined, Backend, Table, Form, Layui, Template) {

    var Controller = {
        index: function () {

            Controller.api.teacherList();

            //学校
            $(document).on('change', '.school', function () {
                Controller.api.teacherList();
            });
            //教师
            $(document).on('click', '.teacher', function () {
                $('.curr').removeClass('curr');
                $(this).addClass('curr');
                
                if ($('.semester_num').val()) {
                    Controller.api.scheduleList();
                } else {
                    Controller.api.semesterList();
                }
            });
            //教师刷新
            $(document).on('click', '.btn-refresh', function () {
                $('.btn-refresh .fa-refresh').addClass('fa-spin');
                Controller.api.teacherList();
            });
            //学期
            $(document).on('change', '.semester', function () {
                Controller.api.semesterWeekList();
            });
            //学期周次
            $(document).on('change', '.semester_num', function () {
                Controller.api.scheduleList();
            });

        },
        api: {
            teacherList: function () {
                $(".semester").html('');
                $(".semester_num").html('');
                $("#schedule").html('');
                $.ajax({
                    type: 'post',
                    url: '/admin/teacher/searchList',
                    cache: false,
                    data: {school_id:$('.school').val()},
                    dataType: 'json',
                    success: function (data) {
                        if (data.code) {
                            $('#teacherList').html(Template('teacherListTpl', data.data));
                        } else {
                            Backend.api.toastr.error(data.msg);
                        }

                        $('.btn-refresh .fa-spin').removeClass('fa-spin');
                    },
                    error: function () {
                        Backend.api.toastr.error('保存失败');
                    }
                });
            },
            semesterList: function () {
                $(".semester_num").html('');
                $("#schedule").html('');
                $.ajax({
                    type: 'post',
                    url: 'semester/select',
                    cache: false,
                    data: {school_id: $('.school').val()},
                    dataType: 'json',
                    success: function (ret) {
                        $(".semester").html(Template("semesterTpl", ret));
                    },
                    error: function () {
                    }
                });
            },
            semesterWeekList: function () {
                $("#schedule").html('');
                $.ajax({
                    type: 'post',
                    url: 'semester/selectWeekList',
                    cache: false,
                    data: {id: $('.semester').val()},
                    dataType: 'json',
                    success: function (ret) {
                        $(".semester_num").html(Template("semesterTpl", ret));
                    },
                    error: function () {
                    }
                });
            },
            scheduleList: function () {
                $.ajax({
                    type: 'post',
                    url: '/admin/course/teacher/index',
                    cache: false,
                    data: {school_id:$('.school').val(),teacher_id:$('.teacher.curr').data('id'),semester_id:$('.semester').val(),semester_num:$('.semester_num').val()},
                    dataType: 'json',
                    success: function (data) {
                        if (data.code) {
                            $('#schedule').html(Template('scheduleTpl', data));
                        } else {
                            Backend.api.toastr.error(data.msg);
                        }
                    },
                    error: function () {
                    }
                });
            },

        }
    };
    return Controller;
});